<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/26 0026
 * Time: 10:07
 */
defined("IN_IA")or exit("Access Denied");
global $_W,$_GPC;
$uniacid=$_GPC['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"getCoupon";
$uid=$_GPC['uid'];
error_reporting(0);
//领取优惠券
if($op=='getCoupon'){
    $cid=$_GPC['cid'];
    $con=array(
        'uniacid'=>$uniacid,
        'cid'=>$cid,
        'uid'=>$uid,
    );
    $userCoupon=pdo_get('cqkundian_farm_user_coupon',$con);
    $couponData=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$cid,'is_delete'=>0));
    if($couponData['count']>0){
        if($couponData['count']>$couponData['use_count']){
            if(empty($userCoupon)){
                $data=array(
                    'cid'=>$cid,
                    'uid'=>$uid,
                    'uniacid'=>$uniacid,
                    'create_time'=>time(),
                );
                $res=pdo_insert('cqkundian_farm_user_coupon',$data);

                pdo_update('cqkundian_farm_shop_coupon',array('use_count +='=>1,'count -='=>1),array('uniacid'=>$uniacid,'id'=>$cid));
                echo $res ?json_encode(array('code'=>1,'msg'=>'success')) : json_encode(array('code'=>2,'msg'=>'error'));die;
            }else{
                echo json_encode(array('code'=>3,'msg'=>'already received'));die;
            }
        }else{
            echo json_encode(array('code'=>4));die;     //优惠券已领完
        }
    }else {  //领取无限制
        if(empty($userCoupon)){
            $data=array(
                'cid'=>$cid,
                'uid'=>$uid,
                'uniacid'=>$uniacid,
                'create_time'=>time(),
            );
            $res=pdo_insert('cqkundian_farm_user_coupon',$data);
            pdo_update('cqkundian_farm_shop_coupon',array('use_count +='=>1),array('uniacid'=>$uniacid,'id'=>$cid));
            echo $res ?json_encode(array('code'=>1,'msg'=>'success')) : json_encode(array('code'=>2,'msg'=>'error'));die;
        }else{
            echo json_encode(array('code'=>3,'msg'=>'already received'));die;
        }
    }
}


//领券中心列表
if($op=='getCouponList'){
    $type=$_GPC['type'];

    $data=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>$type,'is_delete'=>0),'','','rank asc');
    for($i=0;$i<count($data);$i++){
        //查询用户是否已经领取过该优惠券

        if($data[$i]['expiry_date']==2){
            if($data[$i]['end_time']<time()){
                unset($data[$i]);
            }else {
                $data[$i]['begin_time'] = date("Y-m-d", $data[$i]['begin_time']);
                $data[$i]['end_time'] = date("Y-m-d", $data[$i]['end_time']);
            }
        }
        if($data[$i]['type']==1){
            $data[$i]['type_chinese_name']='普通商城可用';
        }elseif ($data[$i]['type']==2){
            $data[$i]['type_chinese_name']='组团商城可用';
        }elseif ($data[$i]['type']==3){
            $data[$i]['type_chinese_name']='畜牧领养可用';
        }elseif ($data[$i]['type']==4){
            $data[$i]['type_chinese_name']='租地可用';
        }elseif ($data[$i]['type']==5){
            $data[$i]['type_chinese_name']='种子购买可用';
        }
        $userCoupon=pdo_get('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid,'cid'=>$data[$i]['id']));
        if(empty($userCoupon)){
            $data[$i]['isget']=1;
        }else{
            $data[$i]['isget']=0;
        }
    }
    $data=array_sort($data,'isget',SORT_DESC);
    $request['couponData']=$data;
    echo json_encode($request);die;
}


//使用优惠券列表
if($op=='getUseCoupon'){
    $type=$_GPC['type'];
    $totalPrice=$_GPC['totalPrice'];
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>0),'','','create_time desc');
    for ($i=0;$i<count($userCoupon);$i++){
        $coupon=pdo_get('cqkundian_farm_shop_coupon',array('id'=>$userCoupon[$i]['cid'],'uniacid'=>$uniacid));

        if($coupon['type']==1){
            $coupon['type_chinese_name']='普通商城可用';
        }elseif ($coupon['type']==2){
            $coupon['type_chinese_name']='组团商城可用';
        }elseif ($coupon['type']==3){
            $coupon['type_chinese_name']='畜牧领养可用';
        }elseif ($coupon['type']==4){
            $coupon['type_chinese_name']='租地可用';
        }elseif ($coupon['type']==5){
            $coupon['type_chinese_name']='种子购买可用';
        }
        if($userCoupon[$i]['status']==0) {
            if ($coupon['type'] == $type) {
                if ($coupon['expiry_date'] == 1) {
                    if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) {
                        if ($coupon['low_cash_price'] < $totalPrice) {  //判断订单金额是否大于优惠消费最低金额
                            $userCoupon[$i]['is_use'] = 1;
                        } else {
                            $userCoupon[$i]['is_use'] = 0;
                        }
                    } else {
                        $userCoupon[$i]['is_use'] = 0;
                    }
                } else {
                    if (time() > $coupon['begin_time'] && time() < $coupon['end_time']) {
                        if ($coupon['low_cash_price'] < $totalPrice) {  //判断订单金额是否大于优惠消费最低金额
                            $userCoupon[$i]['is_use'] = 1;
                        } else {
                            $userCoupon[$i]['is_use'] = 0;
                        }
                    } else {
                        $userCoupon[$i]['is_use'] = 0;
                    }
                }
            } else {
                $userCoupon[$i]['is_use'] = 0;
            }
        }else{
            $userCoupon[$i]['is_use'] = 0;
        }
        if($coupon['expiry_date']==2){
            $coupon['begin_time'] = date("Y-m-d", $coupon['begin_time']);
            $coupon['end_time'] = date("Y-m-d", $coupon['end_time']);
        }
        $userCoupon[$i]['coupon']=$coupon;
    }

    $request['userCoupon'] =array_sort($userCoupon,'is_use',SORT_DESC);
    echo json_encode($request);die;
}

//用户所有优惠券
if($op=='getUserCouponList'){
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid),'','','create_time desc');
    for ($i=0;$i<count($userCoupon);$i++){
        $coupon = pdo_get('cqkundian_farm_shop_coupon', array('uniacid' => $uniacid, 'id' => $userCoupon[$i]['cid']));
        if($userCoupon[$i]['status']==1){
            $userCoupon[$i]['state']=1;  //已使用
        }else{
            //优惠券是否过期
            if ($coupon['expiry_date'] == 1) {
                $coupon['expiry_time']=date("Y-m-d", ($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)));
                if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) { //未过期
                    $userCoupon[$i]['state']=0; //未使用
                }else{
                    $userCoupon[$i]['state']=2;  //已过期
                }
            } else {
                if (time() < $coupon['end_time']) {
                    $userCoupon[$i]['state']=0;
                }else{
                    $userCoupon[$i]['state']=2;
                }
            }
        }
        if($coupon['expiry_date']==2){
            $coupon['begin_time'] = date("Y-m-d", $coupon['begin_time']);
            $coupon['end_time'] = date("Y-m-d", $coupon['end_time']);
        }else{
            $coupon['expiry_time']=date("Y-m-d", ($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)));
        }
        if($coupon['type']==1){
            $coupon['type_chinese_name']='普通商城可用';
        }elseif ($coupon['type']==2){
            $coupon['type_chinese_name']='组团商城可用';
        }elseif ($coupon['type']==3){
            $coupon['type_chinese_name']='畜牧领养可用';
        }elseif ($coupon['type']==4){
            $coupon['type_chinese_name']='租地可用';
        }elseif ($coupon['type']==5){
            $coupon['type_chinese_name']='种子购买可用';
        }
        $userCoupon[$i]['coupon']=$coupon;
    }
    echo json_encode(array('userCoupon'=>$userCoupon));die;
}

//获取用户可用优惠券
if($op=='getUseCouponCount'){
    $type=$_GPC['type'];
    $total_price=$_GPC['totalprice'];
    $request['total_price']=$total_price;
    $couponCount=0;
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>0),'','','create_time desc');
    if(!empty($userCoupon)) {
        for ($i = 0; $i < count($userCoupon); $i++) {
            $coupon = pdo_get('cqkundian_farm_shop_coupon', array('uniacid' => $uniacid, 'id' => $userCoupon[$i]['cid'], 'type' => $type));
            //优惠券是否过期
            if ($coupon['expiry_date'] == 1) {
                if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) {
                    if ($coupon['low_cash_price'] < $total_price) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            } else {
                if (time() > $coupon['begin_time'] && time() < $coupon['end_time']) {
                    if ($coupon['low_cash_price'] < $total_price) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            }
        }
        $request['userCoupon'] =array_sort($userCoupon,'is_use',SORT_DESC);
    }else{
        $request['userCoupon']=array();
    }
    $request['couponCount']=$couponCount;

    echo json_encode($request);die;
}


if($op=='getLandCoupon'){
    //查看用户是否有优惠券
    $couponCount=0;
    $total_price=$_GPC['total_price'];
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>0));
    if(!empty($userCoupon)) {
        for ($i = 0; $i < count($userCoupon); $i++) {
            $coupon = pdo_get('cqkundian_farm_shop_coupon', array('uniacid' => $uniacid, 'id' => $userCoupon[$i]['cid'], 'type' => 4));
            //优惠券是否过期
            if ($coupon['expiry_date'] == 1) {
                if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) {
                    if ($coupon['low_cash_price'] < $total_price) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            } else {
                if (time() > $coupon['begin_time'] && time() < $coupon['end_time']) {
                    if ($coupon['low_cash_price'] < $total_price) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            }
        }
    }
    $request['couponCount']=$couponCount;
    echo json_encode($request);die;
}

//排序
function array_sort($data,$col,$type=SORT_DESC){
//$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序
    $arr=array();
    if(is_array($data)){
        $i=0;
        foreach($data as $k=>$v){
            if(key_exists($col,$v)){
                $arr[$i] = $v[$col];
                $i++;
            }else{
                continue;
            }
        }
    }else{
        return false;
    }
    array_multisort($arr,$type,$data);
    return $data;
}







